import type { IExam as IExamBase, IExamRecord as IExamRecordBase, ExamStatus } from '@shared/types/exam';

// 前端专用的考试类型 - 将 Date 类型转换为 string
export interface IExam extends Omit<IExamBase, 'start_time' | 'end_time' | 'create_time'> {
  start_time: string;
  end_time: string;
  create_time?: string;
}

// 前端专用的考试记录类型 - 将 Date 类型转换为 string
export interface IExamRecord extends Omit<IExamRecordBase, 'start_time' | 'submit_time'> {
  start_time: string;
  submit_time?: string;
}

// 包含详细试卷信息的考试类型
export interface IExamWithPaper extends Omit<IExam, 'paper'> {
  paper: {
    _id: string;
    title: string;
    duration: number;
    totalScore: number;
    question_list: Array<{
      _id: string;
      content: string;
      type: 'Judgement' | 'Select' | 'multiSelect';
      options?: Array<{
        symbol: string;
        content: string;
      }>;
      score: number;
      difficulty: string;
    }>;
  };
}

// 开始考试时的响应类型
export interface IStartExamResponse {
  record_id: string;
  activity: IExamWithPaper;
}

// 导出状态类型
export type { ExamStatus };

// 工具函数：将前端类型转换为后端类型（用于提交数据时）
export const convertExamToBackend = (exam: IExam): IExamBase => ({
  ...exam,
  start_time: new Date(exam.start_time),
  end_time: new Date(exam.end_time),
  create_time: exam.create_time ? new Date(exam.create_time) : undefined,
});

// 工具函数：将前端类型转换为后端类型（用于提交数据时）
export const convertExamRecordToBackend = (record: IExamRecord): IExamRecordBase => ({
  ...record,
  start_time: new Date(record.start_time),
  submit_time: record.submit_time ? new Date(record.submit_time) : undefined,
});

// 工具函数：将后端类型转换为前端类型（用于接收数据时）
export const convertExamFromBackend = (exam: IExamBase): IExam => ({
  ...exam,
  start_time: exam.start_time.toISOString(),
  end_time: exam.end_time.toISOString(),
  create_time: exam.create_time?.toISOString(),
});

// 工具函数：将后端类型转换为前端类型（用于接收数据时）
export const convertExamRecordFromBackend = (record: IExamRecordBase): IExamRecord => ({
  ...record,
  start_time: record.start_time.toISOString(),
  submit_time: record.submit_time?.toISOString(),
});
